var Window = function(f){
	var self = this;
	this.comp = null;
	this.titulo = null;
	
	var tabs = [];
	this.onShow = null;
	var tabOpened = null;

	var create = function(){

	};

	this.addTab = function(name,label){
		tabs[name] = new TabItem(name,label,self);
		return tabs[name];
	};
	this.init = f;
	this.bind = function(e){
		if( !e ){
			this.comp = $("[type='window']");
		}else{
			this.comp = $(e);
		}
		this.comp.addClass("container_16");
		
		var dvTitulo = $('<div class="grid_16" style="height: 20px"/>');
		var spTitulo = $('<h5 style="text-align: center;"/>');
		spTitulo.text("Cadastro de Scripts");
		dvTitulo.append(spTitulo);
		
		var dvTabs = $('div[type=tabs]');
		var dvTabLabels = $('<div class="grid_16"/>');
		var dvTabContents = $('<div class="grid_16"/>');
		dvTabs.prepend(dvTabLabels);
		for( var i in tabs){
			var t = tabs[i];
			dvTabLabels.append(t.getTabLabel());
			t.getComp().hide();
		}
		this.comp.prepend(dvTitulo);
		self.openTabByIndex(0);
	};
	
	var findTab = function(name){
		return tabs[name];
	};
	
	this.openTab = function(n){
		var tab = findTab(n);
		self.showTab(tab);
	};

	this.findTabIndex = function(idx){
		var tab = null;
		var i = 0;
		for( var n in tabs ){
			if( i == idx ){
				tab = tabs[n];
				break;
			}
			i++;
		}
		return tab;
	};
	
	this.openTabByIndex = function(idx){
		var tab = self.findTabIndex(idx);
		self.showTab(tab);
	};
	
	this.showTab = function(tab){
		var tabOld = null;
		if( tabOpened ){
			var tabOld = findTab(tabOpened);
		}
		if( tabOld ){
			tabOld.hide();
		}
		tab.show();
		tabOpened = tab.getName();
		if( self.onShow ){
			self.onShow(tabOpened);			
		}
	};
	
	create();	
};

var TabItem = function(name,label,owner){
	var n = name;
	var l = label;
	var o = owner;
	var comp;
	var components = [];
	var componentsField = [];
	var tabLabel;
	var create = function(){
		comp = $("[type=tab][name="+n+"]");
		tabLabel = $('<div class="tab-label" style="padding:5px;"/>');
		tabLabel.attr("name",n);
		tabLabel.text(l);
		tabLabel.click({n:n},function(e){
			o.openTab(e.data.n);
		});
		
		comp.hide();
	};
	
	this.getLabel = function(){
		return label;
	};
	
	this.getName = function(){
		return name;
	};

	this.getComp = function(){
		return comp;
	};
	
	this.add = function(name,component){
		components[name] = component;
		if( component.hasField ){
			componentsField[name] = component;
		}
		return component;
	};

	this.clearFields = function(){
		for( var n in componentsField ){
			componentsField[n].clear();
		}
	};
	
	this.get = function(name){
		return components[name];
	};

	this.getComponents = function(){
		return components;
	};

	this.getFieldComponents = function(){
		return componentsField;
	};
	
	this.bindAll = function(){
		for( var c in components){
			components[c].bind('#'+ n + '_'+ c);
		}
	};
	
	this.getTabLabel = function(){
		return tabLabel;
	};
	
	this.hide = function(){
		tabLabel.removeClass("active");
		comp.hide();
	};
	
	this.show = function(){
		tabLabel.addClass("active");
		comp.show();
	};
	
	create();
};
